<template>
    <el-dialog v-model="dialogShow12" width="60%">
      <div class="titleShow">
        <div class="titles">{{ $t('licence.sys_safetyParam') }}</div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogShow12 = false">{{ $t('licence.cancel') }}</el-button>
          <el-button type="primary"
            @click="dialogClick12Check">{{ $t('licence.determine') }}</el-button>
        </span>
      </template>
      <span class="safety-right">
        <el-button type="info" @click="getSafetyParamByFirmware()">{{
          $t('licence.loading')
        }}</el-button>
      </span>
      <div class="conten-main-b">
        <ul class="head">
          <li>{{ $t('licence.fau_num') }}</li>
          <li>{{ $t('licence.par_name') }}</li>
          <li>{{ $t('licence.par_resolvingPower') }}</li>
          <li>{{ $t('licence.par_protect') }}</li>
        </ul>
        <ul v-for="item in array">
          <li>{{ item.id }}</li>
          <li>{{ item.name }}</li>
          <li>{{ item.radio }}</li>
          <li>
            <el-input v-model="item.parameters" :placeholder="item.parameters"
              class="inputW" @change="changeParameters(item)"></el-input>
          </li>
        </ul>
      </div>
    </el-dialog>

    <el-dialog v-model="dialogShow12Check" width="30%">
        <div class="titleShow">
          <div class="titles">{{ $t('licence.sys_set_verify') }}</div>
        </div>
        <el-form :inline="true" :model="formInline" ref="formInlines" class="demo-form-inline">
          <el-form-item :label="$t('licence.sys_set_password')" prop="num_12_0">
            <el-input style="width: 300px" v-model="formInline.num_12_0"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogShow12Check = false">{{ $t('licence.cancel') }}</el-button>
            <el-button type="primary" @click="instruction">{{ $t('licence.determine') }}</el-button>
          </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { commandControl } from "@/api/inveter/inveter";
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
import { getSafetyParameter,getSafetyParamByFirmwares } from "@/api/dianzhan/parameter.js"; //保护设置参数
const dialogShow12 = ref(false) //安规参数
const dialogShow12Check = ref(false) //安规参数
const formInline = ref({
  num_12_0: '' as any,
})
const formInlines = ref<ElFormInstance>();
const requestCount =  ref(0)
const props = defineProps(['ids','lang','powerStation','userInfo']);
//保护参数设置保存
let safetyParameterData = ref({
  minCos: '' as any,
  country: '' as any,
  pigMinV: '' as any,
  pigMaxV: '' as any,
  pigMinF: '' as any,
  pigMaxF: '' as any,
  reconnectWait: '' as any,
  startWait: '' as any,
  riseSlope: '' as any,
  puCurveEnable: '' as any,
  puCurveStartV: '' as any,
  puCurveEndV: '' as any,
  puCurveResTime: '' as any,
  puCurveResDelay: '' as any,
  ofEnable: '' as any,
  ofPoint: '' as any,
  ofSlope: '' as any,
  ofResDelay: '' as any,
  reactiveEnable: '' as any,
  quCurveStartUv: '' as any,
  quCurveEndUv: '' as any,
  quCurveStartOv: '' as any,
  quCurveEndOv: '' as any,
  quCurveResTime: '' as any,
  quCurveResDelay: '' as any,
  fixedPowerFactorValue: '' as any,
  reactivePowerRatioValue: '' as any,
  pfCurveStartPercent: '' as any,
  pfCurveEndPercent: '' as any,
  lcEnable: '' as any,
  lcCurveVoltageOne: '' as any,
  lcCurveVoltageOneTime: '' as any,
  lcCurveVoltageTwo: '' as any,
  lcCurveVoltageTwoTime: '' as any,
})
let assign = ()=>{
  array.value = [
            { id: 1, name: t('licence.safety_num1'), radio: '', parameters: safetyParameterData.value.country,key:'country' },
            { id: 2, name: t('licence.safety_num2'), radio: 0.1, parameters: safetyParameterData.value.pigMinV,key:'pigMinV' },
            { id: 3, name: t('licence.safety_num3'), radio: 0.1, parameters: safetyParameterData.value.pigMaxV,key:'pigMaxV' },
            { id: 4, name: t('licence.safety_num4'), radio: 0.01, parameters: safetyParameterData.value.pigMinF,key:'pigMinF' },
            { id: 5, name: t('licence.safety_num5'), radio: 0.01, parameters: safetyParameterData.value.pigMaxF,key:'pigMaxF' },
            { id: 6, name: t('licence.safety_num6'), radio: 1, parameters: safetyParameterData.value.reconnectWait,key:'reconnectWait' },
            { id: 7, name: t('licence.safety_num7'), radio: 1, parameters: safetyParameterData.value.startWait,key:'startWait' },
            { id: 8, name: t('licence.safety_num8'), radio: 0.1, parameters: safetyParameterData.value.riseSlope,key:'riseSlope' },
            { id: 9, name: t('licence.safety_num9'), radio: '', parameters: safetyParameterData.value.puCurveEnable,key:'puCurveEnable' },
            { id: 10, name: t('licence.safety_num10'), radio: 0.1, parameters: safetyParameterData.value.puCurveStartV,key:'puCurveStartV' },
            { id: 11, name: t('licence.safety_num11'), radio: 0.1, parameters: safetyParameterData.value.puCurveEndV,key:'puCurveEndV' },
            { id: 12, name: t('licence.safety_num12'), radio: 1, parameters: safetyParameterData.value.puCurveResTime,key:'puCurveResTime' },
            { id: 13, name: t('licence.safety_num13'), radio: 1, parameters: safetyParameterData.value.puCurveResDelay,key:'puCurveResDelay' },
            { id: 14, name: t('licence.safety_num14'), radio: 1, parameters: safetyParameterData.value.ofEnable,key:'ofEnable' },
            { id: 15, name: t('licence.safety_num15'), radio: 0.01, parameters: safetyParameterData.value.ofPoint,key:'ofPoint' },
            { id: 16, name: t('licence.safety_num16'), radio: 0.1, parameters: safetyParameterData.value.ofSlope,key:'ofSlope' },
            { id: 17, name: t('licence.safety_num17'), radio: 1, parameters: safetyParameterData.value.ofResDelay,key:'ofResDelay' },
            { id: 18, name: t('licence.safety_num18'), radio: '', parameters: safetyParameterData.value.reactiveEnable,key:'reactiveEnable' },
            { id: 19, name: t('licence.safety_num19'), radio: 0.1, parameters: safetyParameterData.value.quCurveStartUv,key:'quCurveStartUv' },
            { id: 20, name: t('licence.safety_num20'), radio: 0.1, parameters: safetyParameterData.value.quCurveEndUv,key:'quCurveEndUv' },
            { id: 21, name: t('licence.safety_num21'), radio: 0.1, parameters: safetyParameterData.value.quCurveStartOv,key:'quCurveStartOv' },
            { id: 22, name: t('licence.safety_num22'), radio: 0.1, parameters: safetyParameterData.value.quCurveEndOv,key:'quCurveEndOv' },
            { id: 23, name: t('licence.safety_num23'), radio: 1, parameters: safetyParameterData.value.quCurveResTime,key:'quCurveResTime' },
            { id: 24, name: t('licence.safety_num24'), radio: 1, parameters: safetyParameterData.value.quCurveResDelay,key:'quCurveResDelay' },
            { id: 25, name: t('licence.safety_num25'), radio: '', parameters: safetyParameterData.value.fixedPowerFactorValue,key:'fixedPowerFactorValue' },
            { id: 26, name: t('licence.safety_num26'), radio: '', parameters: safetyParameterData.value.reactivePowerRatioValue,key:'reactivePowerRatioValue' },
            { id: 27, name: t('licence.safety_num27'), radio: '', parameters: safetyParameterData.value.minCos,key:'minCos' },
            { id: 28, name: t('licence.safety_num28'), radio: '', parameters: safetyParameterData.value.pfCurveStartPercent,key:'pfCurveStartPercent' },
            { id: 29, name: t('licence.safety_num29'), radio: '', parameters: safetyParameterData.value.pfCurveEndPercent,key:'pfCurveEndPercent' },
            { id: 30, name: t('licence.safety_num30'), radio: '', parameters: safetyParameterData.value.lcEnable,key:'lcEnable' },
            { id: 31, name: t('licence.safety_num31'), radio: 0.1, parameters: safetyParameterData.value.lcCurveVoltageOne,key:'lcCurveVoltageOne' },
            { id: 32, name: t('licence.safety_num32'), radio: 1, parameters: safetyParameterData.value.lcCurveVoltageOneTime,key:'lcCurveVoltageOneTime' },
            { id: 33, name: t('licence.safety_num33'), radio: 0.1, parameters: safetyParameterData.value.lcCurveVoltageTwo,key:'lcCurveVoltageTwo' },
            { id: 34, name: t('licence.safety_num34'), radio: 1, parameters: safetyParameterData.value.lcCurveVoltageTwoTime,key:'lcCurveVoltageTwoTime' }
  ]
}
const prepareParam = () => {
  if (props.ids.length != 1) {
    return 0;
  } else {
    return props.ids[0];
  }
}
// 读取固件获取参数
const getSafetyParamByFirmware = async () => {
  let id = prepareParam();
  if (id == 0) {
    ElMessage({
      type: "warning",
      message: t("licence.onlySingle"),
    });
    return
  }
  let data = {
    stationId: props.powerStation.id,
    commandId: 14,
    ids: id,
    lang: props.lang,
  };
  const loading = ElLoading.service({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
  commandControl(data)
    .then(res => {
      setTimeout(() => {
        getSafetyParamByFirmwares({
          id: id,
        }).then(res => {
          safetyParameterData.value = res.data;
          if(Number(res.data.country) == 0){
              requestCount.value++
              if(requestCount.value > 2){
                requestCount.value = 0
                loading.close();
                return ElMessage.error(t('licence.read_error'));
              }
              getSafetyParamByFirmware()
          }
          else {
            requestCount.value = 0;
            loading.close();
          }
          // 遍历数组，更新每个对象的 parameters 属性
          assign()
        }).catch(err => {
          loading.close();
          return
        });
      }, 5000);
    })
    .catch(err => {
      return
    })
}
const changeParameters = (item)=>{
  safetyParameterData.value[item.key]=item.parameters
  console.log('safetyParameterData.value[item.key]:', safetyParameterData.value[item.key])
}
const instruction = async ()=>{
  if (formInline.value.num_12_0 != 123456) {
      ElMessage({
        type: "error",
        message: t("licence.pass_fail"),
      });
      return
    }
    if (
      safetyParameterData.value.country <= 32767 &&
      safetyParameterData.value.pigMinV <= 32767 &&
      safetyParameterData.value.pigMaxV <= 32767 &&
      safetyParameterData.value.pigMinF <= 32767 &&
      safetyParameterData.value.pigMaxF <= 32767 &&
      safetyParameterData.value.reconnectWait <= 32767 &&
      safetyParameterData.value.startWait <= 32767 &&
      safetyParameterData.value.riseSlope <= 32767 &&
      safetyParameterData.value.puCurveEnable <= 32767 &&
      safetyParameterData.value.puCurveStartV <= 32767 &&
      safetyParameterData.value.puCurveEndV <= 32767 &&
      safetyParameterData.value.puCurveResTime <= 32767 &&
      safetyParameterData.value.puCurveResDelay <= 32767 &&
      safetyParameterData.value.ofEnable <= 32767 &&
      safetyParameterData.value.ofPoint <= 32767 &&
      safetyParameterData.value.ofSlope <= 32767 &&
      safetyParameterData.value.ofResDelay <= 32767 &&
      safetyParameterData.value.reactiveEnable <= 32767 &&
      safetyParameterData.value.quCurveStartUv <= 32767 &&
      safetyParameterData.value.quCurveEndUv <= 32767 &&
      safetyParameterData.value.quCurveStartOv <= 32767 &&
      safetyParameterData.value.quCurveEndOv <= 32767 &&
      safetyParameterData.value.quCurveResTime <= 32767 &&
      safetyParameterData.value.quCurveResDelay <= 32767 &&
      safetyParameterData.value.fixedPowerFactorValue <= 32767 &&
      safetyParameterData.value.reactivePowerRatioValue <= 32767 &&
      safetyParameterData.value.pfCurveStartPercent <= 32767 &&
      safetyParameterData.value.pfCurveEndPercent <= 32767 &&
      safetyParameterData.value.lcEnable <= 32767 &&
      safetyParameterData.value.lcCurveVoltageOne <= 32767 &&
      safetyParameterData.value.lcCurveVoltageOneTime <= 32767 &&
      safetyParameterData.value.lcCurveVoltageTwo <= 32767 &&
      safetyParameterData.value.lcCurveVoltageTwoTime <= 32767
      ){
      ElMessageBox.confirm(t("licence.Submit"), t("licence.Tips"), {
        confirmButtonText: t("licence.determine"),
        cancelButtonText: t("licence.cancel"),
        type: "warning",
      })
        .then(async () => {
          let data = {
            commandId: 13,
            ids: props.ids.join(","),
            safetyParameters: safetyParameterData.value,
            lang: props.lang,
          };
          await commandControl(data);
          ElMessage({
            type: "success",
            message: t("licence.success"),
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: t("licence.cancels"),
          });
        });
      dialogShow12.value = false;
      dialogShow12Check.value = false;
    } else {
      ElMessage({
        type: "error",
        message: t("licence.sys_shuzhi3"),
      });
    }
}
const dialogClick12Check = () => {
  formInline.value.num_12_0 = "";
  dialogShow12Check.value = true;
}
let array =ref([])
const openDialog = async () => {
  dialogShow12.value = true;
  // let res = await getSafetyParameter({
  //   code: props.userInfo.dept.countryCode,
  // });
  // safetyParameterData.value = res.data;
  assign()
}
defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
.conten-main-t {
  margin-bottom: 20px;
}
// 弹框
.conten-main-b {
  margin-top: 20px;
  .head {
    background: #f5f8fa;
    height: 56px;
    line-height: 56px;
    // margin-bottom: 20px;

    li {
      font-size: 14px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 600;
      color: #515a6e;
    }
  }

  ul {
    padding: 0;
    display: flex;
    height: 56px;
    line-height: 56px;

    li {
      width: 20%;
      text-align: center;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #606266;

      &:nth-child(1) {
        width: 15%;
      }

      &:nth-child(2) {
        width: 50%;
      }

      &:nth-child(3) {
        width: 15%;
      }
    }
  }
}
.inputW {
  width: 93px;
  // background: #f5f7fb;
  border-radius: 4px;
}
.safety-right {
  display: flex;
  justify-content: flex-end
}
</style>


